from typing import Optional, List
from app.core.crud import CRUDBase
from app.models.system.aiprojects import AIProject
from app.schemas.system.aiprojects import AIProjectCreate, AIProjectUpdate
from tortoise.expressions import Q

class AIProjectService(CRUDBase[AIProject, AIProjectCreate, AIProjectUpdate]):
    def __init__(self):
        super().__init__(model=AIProject)

    async def get_project_by_user(self, project_id: int, user_id: int=None) -> Optional[AIProject]:
        """根据项目ID和用户ID获取项目"""
        return await self.model.filter(id=project_id, is_deleted=False).prefetch_related('user').first()

    async def list_projects_by_user(self, user_id: int, page: int = 1, page_size: int = 10) -> List[AIProject]:
        """根据用户ID获取项目列表"""
        return await self.model.filter(user_id=user_id, is_deleted=False).offset((page-1)*page_size).limit(page_size)

aiprojects_service = AIProjectService()